import flask
from flask import Flask, request
from todo import Todo
from todomanager import TodoManager


app = Flask(__name__, static_folder="a")
app.config['JSON_AS_ASCII'] = False
storage = TodoManager()
storage.load()


@app.route('/')
def respon():
    return '<h1>hello world</h1>'


@app.route('/todo', methods=['GET'])
def get_todo():
    return {'todo': [x.__dict__ for x in TodoManager.todos]}


@app.route('/create_todo', methods=['POST'])
def created():
    if 'title' not in request.json:
        return '参数错误，必须提供title参数', 400
    todo = storage.create(request.json['title'])
    return todo.__dict__


@app.route('/mark_completed', methods=['POST'])
def change():
    if 'id' not in request.json:
        return '参数错误，必须提供id参数', 400
    data = storage.mark_completed(request.json['id'])
    return [x.__dict__ for x in TodoManager.todos]


if __name__ == '__main__':
    app.run()
